#include <stdio.h>
#include <string.h>

/**
 * @todo Assume that one line only contains 80 characters.
 */
#define CONSOLE_ROWS 80

/**
 * Add border character.
 */
void ui_add_border_char(unsigned char count)
{
    for (; count > 0; count--) {
        printf("*");
    }
}

/**
 * Add Empty line.
 */
void ui_add_empty_line(unsigned char count)
{
    for (; count > 0; count--) {
        printf("\n");
    }
}

/**
 * Add line with text on the border.
 * @param title The title
 */
void ui_add_border_line_text(char *title)
{
    unsigned char title_len = strlen(title);

    if (title_len < CONSOLE_ROWS - 4) {
        unsigned char star_count_aside = (CONSOLE_ROWS - 2 - title_len) / 2;

        ui_add_border_char(star_count_aside + (title_len % 2));
        printf(" %s ", title);
        ui_add_border_char(star_count_aside);
    } else {
        printf("* %s *", title);
    }

    printf("\n");
}

/**
 * Add single border line.
 */
void ui_add_border_line_single()
{
    ui_add_border_char(CONSOLE_ROWS);
    printf("\n");
}

/**
 * Add empty row in the box.
 */
void ui_add_border_row_empty(unsigned char count)
{
    for (;count > 0; count--) {
        printf("*");

        for (unsigned char count = CONSOLE_ROWS - 2; count > 0; count--) {
            printf(" ");
        }

        printf("*\n");
    }
}

/**
 * Add option description text in the box.
 */
void ui_add_opt_desc(char *opt_desc)
{
    unsigned char opt_desc_len = strlen(opt_desc);

    if (opt_desc_len < CONSOLE_ROWS - 4) {
        unsigned char space_count_right = CONSOLE_ROWS - 4 - opt_desc_len;

        printf("* %s", opt_desc);

        for (unsigned char count = space_count_right; count > 0; count--) {
            printf(" ");
        }

        printf(" *");
    } else {
        printf("* %s *", opt_desc);
    }

    printf("\n");
}

void ui_cls()
{
    system("CLS");
}
